package com.css.fxfzypg.modules.resultManagement.reportManagement.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.baseData.entity.GasstationData;
import com.css.fxfzypg.modules.baseData.entity.RiskEnterpriseData;
import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.*;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.ReportManagement;
import com.css.fxfzypg.modules.resultManagement.reportManagement.service.ReportManagementService;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import com.css.fxfzypg.web.PlatformSessionContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年10月08日  14:32
 * 报告管理
 */
@RestController
@Validated
@RequestMapping("/report")
public class ReportManagementController {

    @Resource
    private ReportManagementService reportManagementService;


    /***
     * 查询分页
     * @param vo
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(ReportManagementVO vo, HttpServletRequest request) {
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String, Object> page = reportManagementService.getPage(vo, curPage, pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }


    /***
     * 查询任务下的报告信息
     * @param taskId
     * @return
     */
    @GetMapping("/getReportByTaskId")
    public RestResponse getReportByTaskId(@RequestParam("id") String taskId) {
        ReportManagement reportByTaskId = reportManagementService.getReportByTaskId(taskId);
        return ResponseCreator.createSuccessResponse(reportByTaskId);
    }

    /***
     * （重新）生成报告
     * @param vo
     * @return
     */
    @SysLog("生成报告")
    @PostMapping("/createReport")
    public RestResponse createReport(@RequestBody QueryVO vo, HttpServletResponse response) throws Exception {
        ReportManagement report = reportManagementService.createReport(vo.getTaskId(), vo.getType(), vo.getLevel(), vo.getDataSource(), vo.getSimplifiedReportTemplate(), vo.getDeReportTemplate(), vo.getAttachId(), response);
        return ResponseCreator.createSuccessResponse(report);
    }

    /***
     * 上传报告
     * @param file
     * @return
     */
    @PostMapping("/uploadReport")
    public RestResponse uploadReport(@RequestParam("file") MultipartFile file, @RequestParam("type") String type, @RequestParam("level") String level, @RequestParam("dataSource") String dataSource, @RequestParam("taskId") String taskId) {
        return reportManagementService.uploadReport(file, type, level, dataSource, taskId);
    }

    /***
     * 报告下载
     * @param reportId
     * @param type 1:简本 2：详本
     * @param def 1系统默认 2、用户自己的
     * @return
     */
    @PostMapping("/downloadReport")
    public void downloadReport(@RequestParam("reportId") String reportId, @RequestParam("type") String type, @RequestParam("def") String def, HttpServletResponse res) throws IOException {
        reportManagementService.downloadReport(reportId, type, def, res);
    }

    @GetMapping("/getHistoryReportByTaskId")
    public RestResponse getHistoryReportByTaskId(@RequestParam("id") String taskId, HttpServletRequest request) {
        //HistoryUploadReport reportByTaskId =reportManagementService.getHistoryReportByTaskId(taskId);
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String, Object> page = reportManagementService.getHistoryReportByTaskId(taskId, curPage, pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    @GetMapping("/getReportListByTaskId/jb")
    public RestResponse getJbReportListByTaskId(@RequestParam("id") String taskId, HttpServletRequest request) {
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String, Object> page = reportManagementService.getJbReportListByTaskId(taskId, curPage, pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    @GetMapping("/getReportListByTaskId/xb")
    public RestResponse getXbReportListByTaskId(@RequestParam("id") String taskId, HttpServletRequest request) {
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String, Object> page = reportManagementService.getXbReportListByTaskId(taskId, curPage, pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /***
     * 历史报告下载
     * @param reportId
     * @return
     */
    @PostMapping("/downloadHistoryReport")
    public void downloadHistoryReport(@RequestParam("reportId") String reportId, HttpServletResponse res) {
        reportManagementService.downloadHistoryReport(reportId, res);
    }

    /**
     * 提交审核
     *
     * @param map
     */
    @PostMapping("/submitReview")
    public void submitReview(@RequestBody Map<String, String> map) {
        reportManagementService.submitReview(map.get("reportId"));
    }

    /***
     * （重新）生成详本报告
     * @param vo
     * @return
     */
    @PostMapping("/createDetailedReport")
    public RestResponse createDetailedReport(@RequestBody CreateReportParamVO vo, HttpServletResponse response) throws Exception {
        return reportManagementService.createDetailedReport(vo.getTaskId(), vo.getLevel(), vo.getReportName(), vo.getAttachId(), vo.getTaskName(), response);
//        if(msg.indexOf("_")>0){
//            return ResponseCreator.createSuccessResponse(msg.split("_")[0],msg.split("_")[1]);
//        }
//        return ResponseCreator.createSuccessResponse(msg);
    }

//    @GetMapping("/test")
//    public RestResponse test() throws Exception {
//        return reportManagementService.transReportMsgToCpxx();
//    }
    /**
     * 获取地震滑坡模型烈度
     *
     * @param taskId
     */
    @PostMapping("/getSlopeModelIntensity")
    public RestResponse getSlopeModelIntensity(@RequestParam String taskId) {
        String slopeModelIntensity = reportManagementService.getSlopeModelIntensity(taskId);
        return ResponseCreator.createSuccessResponse(slopeModelIntensity);
    }

    /**
     * 获取任务区域内的危化企业
     *
     * @param taskId
     */
    @PostMapping("/getriskData")
    public RestResponse getriskData(@RequestParam String taskId) {
        List<RiskEnterpriseData> list = reportManagementService.getriskData(taskId);
        return ResponseCreator.createSuccessResponse(list);
    }

    /**
     * 获取任务区域内的加油站
     *
     * @param taskId
     */
    @PostMapping("/getGassData")
    public RestResponse getGassData(@RequestParam String taskId) {
        List<GasstationData> list = reportManagementService.getGassData(taskId);
        return ResponseCreator.createSuccessResponse(list);
    }

    /**
     * 获取地震应急能力评估信息
     *
     * @param taskId
     */
    @PostMapping("/getSeiEmeCapAss")
    public RestResponse getSeiEmeCapAss(@RequestParam String taskId) {
        List<SeiEmeCapAssVO> list = reportManagementService.getSeiEmeCapAss(taskId);
        return ResponseCreator.createSuccessResponse(list);
    }

    /**
     * 获取审核通过后的报告信息
     *
     * @return
     */
    @GetMapping("/getReportInfo")
    public List<ReportInfoVO> getReportInfo() {
   /*     RestResponse response = null;
        try{
            List<ReportInfoVO> list=reportManagementService.getReportInfo();
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            //log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;*/
        List<ReportInfoVO> list = reportManagementService.getReportInfo();
        return list;
    }
}
